package com.quanyan.place.wrapper.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlaceEmployee;
import com.quanyan.place.entity.TbCity;
import com.quanyan.place.entity.apireq.ReqEmployeeInfo;
import com.quanyan.place.entity.apiresp.RespCommerceEmployee;
import com.quanyan.place.entity.vo.EmployeeFormDataVo;
import com.quanyan.place.service.BaseService;
import com.quanyan.place.service.EmployeeService;
import com.quanyan.place.service.PlaceBaseService;
import com.quanyan.place.service.PlaceInnerService;
import com.quanyan.place.wrapper.CommerceEmployeeWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.fragment.IFragmentSpec;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangyun on 16-7-6.
 */
@Service("commerceEmployeeWrapperImpl")
public class CommerceEmployeeWrapperImpl implements CommerceEmployeeWrapper {

    @Autowired
    PlaceInnerService placeInnerService;

    @Autowired
    PlaceBaseService placeBaseService;


    @Autowired
    BaseService baseService;

    @Autowired
    EmployeeService employeeService;

    /**
     * 查询商务列表
     *
     * @param reqEmployeeSearch
     * @return
     */
    @Override
    public PageObj<List<RespCommerceEmployee>> getEmployeeInfoList(ReqEmployeeInfo reqEmployeeSearch) {
        Integer pageNum = 0;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        Integer cityId = null;
        String keywords = "";
        if (null != reqEmployeeSearch) {
            if (null != reqEmployeeSearch.getCityId()) {
                cityId = reqEmployeeSearch.getCityId();
            }
            pageNum = reqEmployeeSearch.getPageNum();
            pageSize = reqEmployeeSearch.getPageSize();
            keywords = reqEmployeeSearch.getName();
            if (PlaceConstants.TITLE_BUSSINESS_CITY_MANAGER.equals(reqEmployeeSearch.getTitle())){
                cityId=null;
            }
        }

        List<RespCommerceEmployee> respCommerceEmployeeList = new ArrayList<RespCommerceEmployee>();
        int totalNum = employeeService.countEmployee(cityId, keywords);

        List<TbBizPlaceEmployee> employeeList = employeeService.getEmployeeList(cityId, keywords, pageNum, pageSize, totalNum);
        for (TbBizPlaceEmployee employee : employeeList) {
            RespCommerceEmployee respEmployee = new RespCommerceEmployee();
            respEmployee.setId(employee.getId());
            respEmployee.setCityId(employee.getCityId());
            respEmployee.setUid(employee.getUid());
            respEmployee.setParentId(employee.getParentId());
            respEmployee.setTrueName(employee.getName());
            respEmployee.setTitle(employee.getTitle());
            respEmployee.setMobile(employee.getMobile());
            if (null != employee.getEntryTime()) {
                respEmployee.setEntryTime(DateUtils.convertDateFormatToString(employee.getEntryTime()));
            }
            // 获取上级领导信息
            if (null != employee.getParentId()) {
                TbBizPlaceEmployee parentEmployee = employeeService.getEmployeeById(employee.getParentId());
                respEmployee.setParentName(parentEmployee.getName());
            }
           /* if (null != employee.getCityId()) {
                TbCity city = baseService.getCityById(employee.getCityId());
                respEmployee.setCityName(city.getName());
            }*/
            respCommerceEmployeeList.add(respEmployee);
        }
        PageObj<List<RespCommerceEmployee>> result = PageObj.create(totalNum, pageNum, pageSize, respCommerceEmployeeList);
        return result;
    }

    @Override
    public APIResponse<?> importEmployeeData(EmployeeFormDataVo employeeFormDataVo) {
        TbBizPlaceEmployee vo = new TbBizPlaceEmployee();
       // vo.setDeptId(employeeFormDataVo.getDeptId());
        vo.setName(employeeFormDataVo.getName());
        vo.setParentId(employeeFormDataVo.getParentId());
        vo.setUid(employeeFormDataVo.getUserId());
        vo.setCityId(employeeFormDataVo.getCityId());
        vo.setBasicSalary(employeeFormDataVo.getBasic_salary());
        vo.setEntryTime(employeeFormDataVo.getEntryTime());
        vo.setTitle(employeeFormDataVo.getTitle());
        employeeService.importEmployeeData(vo);
        return APIResponse.returnSuccess();

    }

    @Override
    public APIResponse<?> updateEmployeeById(EmployeeFormDataVo employeeFormDataVo) {
        employeeService.updateEmployeeById(employeeFormDataVo);
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse<?> getEmployeeByUid(Integer uid) {
        return APIResponse.returnSuccess(employeeService.getEmployeeByUid(uid));
    }

    @Override
    public EmployeeFormDataVo getEmployeeById(Integer id) {
          EmployeeFormDataVo reVo=new EmployeeFormDataVo();
          TbBizPlaceEmployee vo= employeeService.getEmployeeById(id);
            reVo.setTitle(vo.getTitle());
            reVo.setCityId(vo.getCityId());
            reVo.setEntryTime(vo.getEntryTime());
            reVo.setUserId(vo.getUid());
           TbBizPlaceEmployee employee= employeeService.getEmployeeById(vo.getParentId());
            reVo.setParentId(vo.getParentId());
        if (employee!=null) {
            reVo.setParentName(employee.getName());
        }
            reVo.setName(vo.getName());
            reVo.setUserId(vo.getUid());
            reVo.setId(vo.getId());
            return reVo;

    }

    @Override
    public APIResponse<?> deleteEmployeeById(Integer id) {
        return employeeService.deleteEmployeeById(id);
    }

    @Override
    public Integer getEmployeeTitleByUid(Integer uid) {
        return employeeService.getEmployeeTitleByUid(uid);
    }

    /**
     * 新增（修改）商务员工
     * @param employeeFormDataVo
     * @return
     */
    @Override
    public APIResponse saveOrUpdateEmployee(EmployeeFormDataVo employeeFormDataVo) {
        if (null != employeeFormDataVo){
            if (employeeFormDataVo.getId() != null){
                employeeService.updateEmployeeById(employeeFormDataVo);
            } else {
                TbBizPlaceEmployee employee = new TbBizPlaceEmployee();
                employee.setName(employeeFormDataVo.getName());
                employee.setParentId(employeeFormDataVo.getParentId());
                employee.setUid(employeeFormDataVo.getUserId());
                employee.setCityId(employeeFormDataVo.getCityId());
                employee.setBasicSalary(employeeFormDataVo.getBasic_salary());
                employee.setEntryTime(employeeFormDataVo.getEntryTime());
                employee.setMobile(employeeFormDataVo.getMobile());
                employee.setTitle(employeeFormDataVo.getTitle());
                employeeService.importEmployeeData(employee);
            }
        }
        return APIResponse.returnSuccess();
    }
}

